﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TS2015.Models;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
using System.Data.Entity;

namespace TS2015.Areas.Admin.Controllers
{
    public class HighSchoolManagementController : Controller
    {
        TS2015Context db = new TS2015Context();

        // GET: Admin/HighSchoolManagement
        public JsonResult GetHighSchool()
        {
            return Json(db.TbTruongs.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult HighSchool()
        {
            Database.SetInitializer<TS2015Context>(null);

            ViewBag.TbTinh = db.TbTinhs.ToList();

            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult EditingPopup_ReadHighSchool([DataSourceRequest] DataSourceRequest request)
        {
            return Json(db.TbTruongs.ToDataSourceResult(request));
        }
        public ActionResult EditingPopup_CreateHighSchool([DataSourceRequest] DataSourceRequest request, TbTruong data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.TbTruongs.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_UpdateHighSchool([DataSourceRequest] DataSourceRequest request, TbTruong data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_DestroyHighSchool([DataSourceRequest] DataSourceRequest request, TbTruong data)
        {
            if (data != null)
            {
                TbTruong itemdata = db.TbTruongs.First(item => item.MaTruong == data.MaTruong);
                db.TbTruongs.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        
    }
}